do ${main}/dos/alex_utility15.do

cd "${supp_data}\shed"

clear
set obs 1
gen blank=1
save temp, replace

foreach yr in 2015 2016 2017 2018 2019 {
use public`yr', clear
foreach x of varlist * {
	local x1 = lower("`x'")
	capture rename `x' `x1'
}
gen caseid`yr' = caseid
gen year = `yr'
append using temp
save temp, replace
}

cd "${main}"
sort caseid2017
order year caseid*
sort caseid2017 year

egen nid2017 = count(year), by(caseid2017)
egen nid2018 = count(year), by(caseid2018)
gen nk = ppt01+ppt25+ppt612+ ppt1317
tab nk, m
 
gen i_worse = b3<=2
replace i_worse = . if b3<0

gen i_nocred = ( a1_a==1)
replace i_nocred = . if a0b<0 | a1_a<0

gen i_nocred2 = ( a1_a==1 | a1_b==1 | a1_c==1 | a2==1)

gen c_oldestkid = 1 if ppt01>0 & max(ppt25, ppt612, ppt1317)==0
replace c_oldestkid = 5 if  ppt25>0 & max(ppt612, ppt1317)==0
replace c_oldestkid = 12 if  ppt612>0 & ppt1317==0
replace c_oldestkid = 17 if  ppt1317>0 
replace c_oldestkid = 0 if nk==0
tab c_oldestkid

gen i_kid01 = ppt01>0
gen i_group = 0 if nk==0
replace i_group = 1 if ppt01>0
replace i_group = 2 if ppt25>0 & ppt01==0

gen i_group2 = 0 if nk==0
replace i_group2 = 1 if ppt01>0 | ppt25>0

gen i_under50k = ppincimp<=11


gen w = weight_pop if year ==2019
replace w = weight2b if year==2018
replace w = weight3b if year<=2017


regrunner "reg /y\ i.c_oldestkid  i.ppagecat i.ppreg9 i.year [pw=w] if i_under50==/s\, robust", ///
                 runs{y[i_worse i_nocred] s[1] } ///
                 coeffs{k0[0.c_oldestkid] k1[1.c_oldestkid] k2[5.c_oldestkid] k3[12.c_oldestkid] k4[17.c_oldestkid] } ///
                 stats{r2[e(r2_a), ] mean[r(mean), summ /y\ if e(sample)] } ///
                 dataout{${results}/shed_out} fileout{${results}/shed} sheetout{out} silent

				 
use ${results}/shed_out, clear
reshape long b_k se_k stars_k p_k , i(y s) j(cat)

label define CAT 0 "No Children" 1 "Age 0-1" 2 "Age 2-5" 3 "Age 6-12" 4 "Age 13-17"
label values cat CAT

gen u_ci95 = b+1.96*se_k
gen l_ci95 = b-1.96*se_k

set scheme s1manual
graph twoway rcap u_ci95 l_ci95 cat if s=="1" & y=="i_worse" , color(black) yline(0, lcolor(black)) ///
			|| scatter b_k cat if s=="1" & y=="i_worse", color(black)  connect(l) xlabel(#5 , valuelabel labsize(small) angle(45)) xtitle("Oldest Child", size(small)) ytitle("Worse Off Financially than 12 Months Ago", size(small))	legend(off)	

graph export ${results}/worse.pdf, replace			

			
			
graph twoway rcap u_ci95 l_ci95 cat if s=="1" & y=="i_nocred" , color(black) yline(0, lcolor(black)) ///
			|| scatter b_k cat if s=="1" & y=="i_nocred", color(black)  connect(l) xlabel(#5 , valuelabel labsize(small) angle(45)) xtitle("Oldest Child", size(small)) ytitle("Turned Down for Credit in Past 12 Months", size(small))	legend(off)	

graph export ${results}/nocred.pdf, replace




table nk if i_k1==1 & ppincimp<=12 , contents(mean i_e400 count i_e400 )
table nk if i_dnk==1 & ppincimp<=12 , contents(mean i_e400 count i_e400  )


tab i_k1 , m
browse if i_k1==1